#!/usr/bin/python
#
#

import sys

PYTHON_VERSION = sys.version_info

import telnetlib


class zyxTelnet(telnetlib.Telnet):
    def __init__(self, host=None, port=0, user=None, password=None,
                    timeout=30, expect_timeout=10):
        if PYTHON_VERSION < (2, 6):
            telnetlib.Telnet.__init__(self, host, port)
        else:
            telnetlib.Telnet.__init__(self, host, port, timeout)

        self.expect_timeout = expect_timeout
        self.vendor = 'zyxel'
        self.__enable = None
        self.command = None
        if not user and not password:
            self.__user = None
            self.__password = None
        elif not user:
            self.__user = None
            self.__password = password
        elif not password:
            self.__user = user
            self.__password = None
        else:
            self.__user = user
            self.__password = password
            self.Login()

    def setUser(self, user):
        self.__user = user

    def setPassword(self, password):
        self.__password = password

    def setEnable(self, enable):
        self.__enable = enable

    def Login(self):
        prompts = ['# ', '> ']
        if self.__user and self.__password:
            user_prompt = self.expect(["User name: "], self.expect_timeout)
            if not user_prompt[1]:
                raise RuntimeError, \
                    'Got "%s" (waiting for username prompt).' % user_prompt[2]
            self._Write('%s\n' % self.__user, ["Password: "])
            prompt = self._Write('%s\n' % self.__password, prompts)
            if prompts[prompt[0]] == '# ':
                self._Write('disable\n', ['> '])
            else:
                # print "Already in unpriv."
                pass
        else:
            raise ValueError

    def Enable(self, enable=None):
        if enable:
            self.__enable = enable
        if self.__enable:
            prompt = self._Write('enable\n', ["Password: "])
            secprompt = self._Write('%s\n'%self.__enable, ["# "])
        else:
            raise ValueError

    def setCommand(self, command):
        self.__command = command

    def Execute(self, command, verbose=True):
        return self._Write('%s\n'%command, ['# ', '> '])

    def _Write(self, text, expected, timeout=None):
        self.write('%s' % text)
        responce = self.expect(expected, timeout or self.expect_timeout)
        if not responce[1]:
            raise RuntimeError, \
                    'Unexpected responce: expected "%s", got "%s".' % (
                                    expected, responce[2])
        else:
            return responce


if __name__ == '__main__':

    import getpass

    user = raw_input('Enter user name: ')  
    password= getpass.getpass('Enter password: ')
    enable = getpass.getpass('Enter enable secret: ')

    addrs = [
                # ...
            ]

    for addr in addrs:
        print 'Getting startup config from %s...' % addr,
        ct = zyxTelnet(addr, user=user, password=password)
        ct.Enable(enable)
        config = ct.Execute('show run')[2]
        open('config.%s' % addr, 'w').write(config)
        print ' done.'

